package org.shangrila.app.sys.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.event.CellEditEvent;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.TabChangeEvent;
import org.shangrila.app.sys.eo.SysAuthority;
import org.shangrila.app.sys.eo.SysMenus;
import org.shangrila.app.sys.eo.SysResource;
import org.shangrila.comm.utils.MsgsUtil;
import org.shangrila.comm.dao.DbAccess;
 
@Named
@SessionScoped
public class SysResourceMB implements Serializable {
	private static final long serialVersionUID = -5951492023664217108L;
	private List<SysResource> voSysResource;
    private SysResource sysResource;
    private SysResource selectedResource;
    
	private List<SysMenus> voSysMenus;
	
    private SysMenus selectedMenu = new SysMenus();
	private List<SysMenus> selectedMenus=new ArrayList<SysMenus>();
    
    private DbAccess db = new DbAccess();
    
    @PostConstruct
    public void init() {
    	newSysResource();
    	voSysResource = db.findEntityAll("SysResource");
    }
         
    public void newSysResource() {
		sysResource = new SysResource();
		sysResource.setCategory("1");
		sysResource.setFlag("1");
		sysResource.setIcon("ui-icon-star");
		sysResource.setSource("/pages/sys/");
    }
    
    public void onTabChange(TabChangeEvent event) {
    	String tabid =  event.getTab().getId();
    	//MsgsUtil.showMessages("Tab Changed", "Active Tab: " + tabid);
    	if("tab2".equals(tabid)) {
        	voSysMenus = db.findEntityAll("SysMenus");
    	}
    }
    
	public String commitAction() {
		
		sysResource.setFlag("1");

		//插入权限记录
		SysAuthority sysAuthority = new SysAuthority();
		sysAuthority.setId(sysResource.getId());
		sysAuthority.setFlag(sysResource.getFlag());
		sysAuthority.setName(sysResource.getName());
		sysAuthority.setDescription(sysResource.getName());

		List<SysAuthority> sysAuthorities = new ArrayList<SysAuthority>();
		sysAuthorities.add(sysAuthority);

		//插入资源记录
		sysResource.setSysAuthorities(sysAuthorities);

		//插入菜单记录
		
		SysMenus sysMenus = new SysMenus();
		sysMenus.setId(sysResource.getId());
		sysMenus.setFlag(sysResource.getFlag());
		sysMenus.setName(sysResource.getName());
		sysMenus.setDescription(sysResource.getName());
		sysMenus.setSysAuthority(sysAuthority);
		sysMenus.setOrderby(sysResource.getId());
		sysMenus.setPid(sysResource.getId());
		
		db.beginTransaction();
		db.insert(sysAuthority);
		db.insert(sysResource);
		db.insert(sysMenus);
		db.commitTransaction();
		
		addMessage("成功", "资源信息录入成功");
		
		voSysResource.add(sysResource);
		newSysResource();
		return null;
	}
	
	public void addMessage(String summary, String detail) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	
      
    public void onRowEdit(RowEditEvent event) {
    	selectedResource = voSysResource.get(voSysResource.indexOf((SysResource) event.getObject()));
    	db.beginTransaction();
    	db.update(selectedResource);
    	db.commitTransaction();
    	MsgsUtil.showMessages("编辑", "编辑保存成功。");
    }
     
    public void onRowCancel(RowEditEvent event) {
    	MsgsUtil.showMessages("编辑", "取消编辑成功。");
    }
     
    public void onCellEdit(CellEditEvent event) {
        Object oldValue = event.getOldValue(); 
        Object newValue = event.getNewValue();
         
        if(newValue != null && !newValue.equals(oldValue)) {
        	selectedMenu = voSysMenus.get(event.getRowIndex());        	
        	db.beginTransaction();
        	db.update(selectedMenu);
        	db.commitTransaction();
        	MsgsUtil.showMessages("编辑", "单元格编辑保存成功。");
        }
    }

	public List<SysResource> getVoSysResource() {
		return voSysResource;
	}

	public void setVoSysResource(List<SysResource> voSysResource) {
		this.voSysResource = voSysResource;
	}

	public SysResource getSysResource() {
		return sysResource;
	}

	public void setSysResource(SysResource sysResource) {
		this.sysResource = sysResource;
	}

	public SysResource getSelectedResource() {
		return selectedResource;
	}

	public void setSelectedResource(SysResource selectedResource) {
		this.selectedResource = selectedResource;
	}
    public List<SysMenus> getVoSysMenus() {
		return voSysMenus;
	}

	public void setVoSysMenus(List<SysMenus> voSysMenus) {
		this.voSysMenus = voSysMenus;
	}

	public SysMenus getSelectedMenu() {
		return selectedMenu;
	}

	public void setSelectedMenu(SysMenus selectedMenu) {
		this.selectedMenu = selectedMenu;
	}

	public List<SysMenus> getSelectedMenus() {
		return selectedMenus;
	}

	public void setSelectedMenus(List<SysMenus> selectedMenus) {
		this.selectedMenus = selectedMenus;
	}
    
}